﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Data;
using System.Diagnostics;

namespace RStein.PosterousReader.WP.Behaviors
{
  public class TextBoxTextChangedUpdateSourceBehavior : TextChangedBehaviorBase<TextBox>
  {
    public TextBoxTextChangedUpdateSourceBehavior()
    {            
    }
    
    
    
    protected override void OnAttached()
    {
      base.OnAttached();
      if (AssociatedObject != null)
      {
        AssociatedObject.TextChanged += AssociatedObject_TextChanged;
      }
    }

    protected override void OnDetaching()
    {
      AssociatedObject.TextChanged -= AssociatedObject_TextChanged;

    }
    
    void AssociatedObject_TextChanged(object sender, TextChangedEventArgs e)
    {
      UpdateSource();
      RunTextChangedAction();
    }

    public override BindingExpression GetBindingExpression(TextBox txtBox)
    {
      if (txtBox == null)
      {
        return null;
      }

      return txtBox.GetBindingExpression(TextBox.TextProperty);
    }
      
   
  }
}
